//hello,2,ok,0,bye,0,test,0,one,1,two,1,a,0
// 首先尝试将内容转换为树结构, {name, count, child:[], deep}
// 怎么做呢, 之前想的是 使用dfs的方式, 子元素, 递归到底, 把自己传给上级
// 先构建树吧, 做好把深度, 也传给父级, 至于之前的内容层级输出, 使用pq可以解决
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

class TreeNode {
    constructor(name, count, child = [], deep = 0) {
        this.name = name
        this.count = count
        this.child = child
        this.deep = deep
    }
}

let initArr = [];
rl.on("line", (line)=>{
    if(initArr.length === 0) {
        initArr = line.trim().split(",")
        getResult(initArr)

        // clear
        initArr.length = 0
    }

})

function getResult(initArr) {
    let treeArr = []
    let index = 0
    while(index < initArr.length - 1){
        let root = new TreeNode(initArr[index++], initArr[index++])
        dfs(root)
         treeArr.push(root)
    }
    let maxDeep = treeArr.reduce((acc, tree) => Math.max(acc, tree.deep) ,0)
    console.log(maxDeep)

    while(treeArr.length) {
        let length = treeArr.length
        let arr = []
        for (let i = 1; i <= length ; i++) {
            let {name, child} = treeArr.shift()
            arr.push(name)
            if(child.length) {
                treeArr.push(...child)
            }
        }
        console.log(arr.join(" "))
    }

    /**
     *
     * @param root {TreeNode}
     * @return {TreeNode || null}
     */
    function dfs(root) {
        if(index >= initArr) return null

        let childArr = []
        let deep = 0
        for (let i = 1; i <= root.count ; i++) {
            let child = dfs(new TreeNode(initArr[index++], initArr[index++]))
            child && childArr.push(child)
            deep = Math.max(deep, child.deep || 0)
        }
        root.child = childArr
        root.deep = deep + 1
        return root
    }
}


